﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeatherStationSys.Common;
using WeatherStationSys.Interface;

namespace WeatherMonitoringSystem
{
    public class TemperatureHiloDataProxy : IHiLoData, IObserver, IAlarmListener
    {
        private IPersistent _iPersistent;
        private TemperatureHiloData _temperatureHiloData;
        private string _type;
        private string _storageKey;
        private AbstractTemperatureSensor _abstractTemperatureSensor;

        /// <summary>
        /// Ctor
        /// </summary>
        public TemperatureHiloDataProxy(StationTookit st, AlarmClock ac, string type, DateTime theDate, double init, long initTime)
        {
            _type = type;
            _storageKey = CalculateStorageKey(theDate);
            _iPersistent = st.GetPersistent();//创建出具体硬件平台的持久化
            _temperatureHiloData = new TemperatureHiloData(st, ac, type, theDate, init, initTime);
            //1.注册进温度传感器
            _abstractTemperatureSensor = st.MakeTemperature();
            _abstractTemperatureSensor.AddObserver(this);
            //2.注册进AlarmClock
            ac.WakeEveryDay(this);
        }

        /// <summary>
        /// 得到最大值
        /// </summary>
        /// <returns></returns>
        public double GetHeightValue() => _temperatureHiloData.GetHeightValue();
       
        /// <summary>
        /// 得到最大值的时间
        /// </summary>
        /// <returns></returns>
        public long GetHeightTime() => _temperatureHiloData.GetHeightTime();
      
        /// <summary>
        /// 得到最小值的时间
        /// </summary>
        /// <returns></returns>
        public long GetLowerTime() => _temperatureHiloData.GetLowerTime();

        /// <summary>
        /// 得到最小值
        /// </summary>
        /// <returns></returns>
        public double GetLowerValue() => _temperatureHiloData.GetLowerValue();

        /// <summary>
        /// 计算储存的Key
        /// </summary>
        /// <returns></returns>
        public string CalculateStorageKey(DateTime theDate)
        {
            string key = _type + theDate.ToString("yyyyMMdd");

            return key;
        }

        /// <summary>
        /// 当前温度
        /// </summary>
        /// <param name="value">当前值</param>
        /// <param name="time">当前时间</param>
        public bool CurrentReading(double currentValue, long time)
        {
            bool isChanged = false;
            isChanged = _temperatureHiloData.CurrentReading(currentValue, time);
            if (isChanged)
            {
                //持久化数据
                Store();
            }

            return isChanged;
        }

        /// <summary>
        /// 持久化数据
        /// </summary>
        public void Store()
        {
            _iPersistent.Store<TemperatureHiloData>(_storageKey, _temperatureHiloData);
        }

        /// <summary>
        /// 新的1天
        /// </summary>
        /// <param name="initial"></param>
        /// <param name="time"></param>
        public void NewDay(double initial, long time)
        {
            _temperatureHiloData.NewDay(initial, time);
            _storageKey = CalculateStorageKey(DateTime.Now);
            Store();
        }

        /// <summary>
        /// 老版本持久化数据
        /// </summary>
        /// <param name="value">当前值</param>
        /// <param name="time">当前时间</param>
        public void DeformedVeiosnCurrentReading(double currentValue, long time)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 温度有变化会调用Update方法
        /// 持久化数据
        /// </summary>
        /// <param name="obj"></param>
        public void Update(object obj)
        {
            CurrentReading(Convert.ToDouble(obj), DateUtility.ConvertToTimeStamp(DateTime.Now));
        }

        /// <summary>
        /// 唤醒
        /// 到午夜了，要去读取温度了
        /// </summary>
        public void Wakeup()
        {
            double temp = _abstractTemperatureSensor.Read();
            NewDay(temp, DateUtility.ConvertToTimeStamp(DateTime.Now));
        }
    }
}
